﻿using Module.NumberGame.Common;
using Prism.Events;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Module.NumberGame.Views
{
    /// <summary>
    /// Interaction logic for NumberGame
    /// </summary>
    public partial class NumberGameApp : UserControl
    {
        const int TotalRow = 5;
        const int MaxNumber = 4;
        bool IsMouseLeftDown;
        bool IsMouseLeftUp;
        double DownX;
        double DownY;
        double OffsetX = 0, OffsetY = 0;
        NumberBox[,] Pieces = new NumberBox[TotalRow, TotalRow];

        private readonly IEventAggregator Ea;
        public NumberGameApp(IEventAggregator ea)
        {
            InitializeComponent();
            this.Ea = ea;
            for (int i = 0; i < TotalRow; i++)
            {
                for (int j = 0; j < TotalRow; j++)
                {
                    Pieces[i, j] = new();
                    Pieces[i, j].SetPosition(i, j);
                    //MainBoard.Children.Add(Pieces[i, j]);
                }
            }
        }

        private void PreMouseDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseLeftDown = true;
            IsMouseLeftUp = false;
            DownX = e.GetPosition(this).X;
            DownY = e.GetPosition(this).Y;
        }

        private void PreMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMouseLeftDown && !IsMouseLeftUp && (Math.Abs(OffsetX) > 20 || Math.Abs(OffsetY) > 20))
            {
                Ea.GetEvent<NumberMessageEvent>().Publish(new NumberMessage() { MouseDisX = OffsetX, MouseDisY = OffsetY });
            }
            IsMouseLeftDown = false;
            IsMouseLeftUp = true;
            OffsetX = 0; OffsetY = 0;

        }

        private void PreMouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseLeftDown && !IsMouseLeftUp)
            {
                OffsetX = e.GetPosition(this).X - DownX;
                OffsetY = e.GetPosition(this).Y - DownY;
            }
        }

        private void ReStart(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < TotalRow; i++)
            {
                for (int j = 0; j < TotalRow; j++)
                {
                    
                }
            }
        }
    }
}
